home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: "Initializer must be a constant expression"...?
- Date: 04 Feb 1996 17:06:59 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb4100659@qcd.lanl.gov>
- References: <311280AA.15FB7483@tulsa.lgc.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Glenn Carr's message of Fri, 02 Feb 1996 15:22:50 -0600
-
- In article <311280AA.15FB7483@tulsa.lgc.com> Glenn Carr
- <gcarr@tulsa.lgc.com> writes:
- <snip>
- We recently switched compilers from gcc to xlc on AIX and we're now
- getting the
- following compilation warnings...
-
- "../combobox.c", line 244.10: 1506-221 (E) Initializer must be a
- constant expression.
- "../combobox.c", line 245.10: 1506-221 (E) Initializer must be a
- constant expression.
- "../combobox.c", line 246.10: 1506-221 (E) Initializer must be a
- constant expression.
-
- in the following...
-
- typedef struct {
- char **var;
- char *descr;
- } cfgStrSpec_t;
-
- char *pszDepCodeSetKey = NULL;
- char *pszDepComboBox = NULL;
- char *pszShowAllIfEmpty = NULL;
- cfgStrSpec_t pCfgStr[] =
- {
- 244: {&pszDepCodeSetKey, "Dependent code_set_key"},
- 245: {&pszDepComboBox, "Dependent widget (combobox)"},
- 246: {&pszShowAllIfEmpty, "Boolean (nonzero or 0)"},
- {NULL, NULL},
- };
-
- Is that not valid, or ANSI C?
-
- Depends on whether or not the segment appears in global
- scope. Initializers for aggregate types and static duration objects
- need to be compile time (I use it loosely: it may include `link time')
- constnt expressions (the ANSI C standard never requires the compiler
- to build an aggregate from scratch at run time automatically. It also
- specifies that all static duration objects be initialized before
- program execution begins). Automatic variables are allocated at each
- invocation of the block to which they belong, and hence their address
- is not a compile time constant.
-
- If the above appears in global scope it is valid. If they appear in a
- block, it is invalid. If there is no need for them to be automatic,
- but block scoping is desired, the declarations of psz... should be
- made `static'.
-
- Also beware that initializing a char* variable from a literal string
- is dangerous because attempt to modify a string literal leads to
- undefined behaviour which may not be diagnosed. It is good practice to
- declare the variables const char* when feasible.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-